package cn.qdhlm.collectservice.core.component;

import cn.qdhlm.collectservice.common.util.CodeUtil;


import cn.qdhlm.collectservice.core.component.analyse.*;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Name: Analyser
 * @Description: Analyser
 * @Author: jiangcs
 */
@Log4j2
@Component
public class Analyser {
    @Autowired
    private AnyAfn00 anyAfn00;
    @Autowired
    private AnyAfn02 anyAfn02;
    @Autowired
    private AnyAfn03 anyAfn03;
    @Autowired
    private AnyAfn05 anyAfn05;
    @Autowired
    private AnyAfn0c anyAfn0c;
    @Autowired
    private AnyAfn0d anyAfn0d;



    /**
     * 解析报文
     *
     * @param deviceDataId
     * @param byteBuf
     */
    public void analysis(String deviceDataId, ByteBuf byteBuf) {
        log.info("analysis[{}] begin ...", deviceDataId);

        // 跳过7个字节: 68(1)L(2)L(2)68(1)C(1)
        byteBuf.skipBytes(7);
        // 设备类型码
        int deviceType = byteBuf.readByte();
        // 协议版本
        String protocolVersion = byteBuf.readByte() + "." + CodeUtil.byteToHexString(byteBuf.readByte());
        // 主站地址和组地址标志
        byteBuf.skipBytes(2);
        // 功能码：AFN
        String afn = CodeUtil.byteToHexString(byteBuf.readByte());
        // SEQ：FF
        String seq = CodeUtil.byteToHexString(byteBuf.readByte());
        // DA
        int da = byteBuf.readUnsignedShort();
        // DT：大端序，1=>256
        int dt = byteBuf.readUnsignedShort();
        // sn
        int sn = byteBuf.readShort();
        // CUSTOMER_ID： 2017-12-07 改为预留字段，采集器编号全局唯一
        byteBuf.skipBytes(2);
        // COLLECTOR_NO
        String collectorNo = CodeUtil.byteToHexString(byteBuf.readByte())
                + CodeUtil.byteToHexString(byteBuf.readByte())
                + CodeUtil.byteToHexString(byteBuf.readByte())
                + CodeUtil.byteToHexString(byteBuf.readByte())
                + CodeUtil.byteToHexString(byteBuf.readByte());

        log.info("analysis: protocolVersion[{}], afn[{}], seq[{}], da[{}], dt[{}], sn[{}], collectorNo[{}], byteBuf[{}]", protocolVersion, afn, seq, da, dt, sn, collectorNo, ByteBufUtil.hexDump(byteBuf));

        try {
            boolean analysisFlag = false;
            switch (afn) {
                // 确认∕否认（AFN=00H）
                case "00":
                    analysisFlag = anyAfn00.execute(da, dt, sn,collectorNo, byteBuf);
                    break;
                // 链路接口检测（AFN=02H）
                case "02":
                    analysisFlag = anyAfn02.execute(da, dt, collectorNo, byteBuf);
                    break;
                // 运行参数（AFN=03H）
                case "03":
                    analysisFlag = anyAfn03.execute(da, dt, collectorNo, byteBuf);
                    break;
                // 读参数（AFN=05H）
                case "05":
                    analysisFlag = anyAfn05.execute(da, dt, sn, collectorNo, byteBuf);
                    break;
                // 请求1类数据（AFN=0CH）
                case "0c":
                    analysisFlag = anyAfn0c.execute(da, dt, sn, collectorNo, byteBuf);
                    break;
                // 请求2类数据（AFN=0DH）
                case "0d":
                    analysisFlag = anyAfn0d.execute(da, dt, sn, collectorNo, byteBuf);
                    break;
                default:
                    log.error("analysis: afn[{}] match fail", afn);
            }

            log.info("analysis result: {}", analysisFlag);

            if (analysisFlag) {
                // 回复确认
//                replyConfirm(deviceType, afn, da, dt, sn, collectorNo);
            }
        } catch (Exception e) {
            log.error("analysis exception: ", e);
        }
        log.info("analysis[{}] end ...", deviceDataId);
    }


}
